﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyIni;

namespace CadStrategy2013.CgqStyle
{
    public class KgbClass:Cgq2013
    {
        string m_ceng;
        int m_bNo;
        public KgbClass(string secname)
            : base(secname)
        {
        }
        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("开入板");
        //}
        public override List<string> ReadTemplateDxf()
        {
            return ReadDxf(Path + "\\template\\开入板.dll");
        }
        public override string GetSaveFileName(int index)
        {
            if (ShiJiTuZhiCount == 1)
                return "\\开入板.dxf";
            return "\\开入板" + (index + 1).ToString() + ".dxf";
            //if (index == 0)
            //    return "\\开入板.dxf";
            //else
            //    return "\\开入板" + index + ".dxf";
        }
        public override DxfDataClass GetDxfData()
        {
            DxfDataClass dxfDataClass = new DxfDataClass();

            IniFile inifile = new IniFile("./\\" + StationName + "\\kgb.txt");
            inifile.Load();
            string strtemp= inifile.GetString("开关板", "板号");
            try
            {
                int l = strtemp.IndexOf('-');
                m_ceng = strtemp.Substring(0, l);
                int.TryParse(strtemp.Substring(l + 1, strtemp.Length - l-1), out m_bNo);
            }
            catch
            {
                m_ceng = "";
                m_bNo = 0;
            }
            int kgbsum0 = inifile.GetInt("开关板", "开关板数量", 0);
       

            for (int i = 0; i < kgbsum0; i++)
            {

                int n1 = GetZPW2000AKgl(dxfDataClass, "开关板" + (i + 1), inifile);
                for(int j=1;j<=48-n1;j++)
                {
                     DevClass devClass = new DevClass();
                     string strs = inifile.GetString("开关板"+(i+1),j.ToString());
                     string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                     int n = s.Length;
                     try
                     {
                         devClass.DevName = s[0];
                     }
                     catch 
                     {
                         devClass.DevName = "";
                     }
                     try
                     {
                         devClass.peiList.Add(s[1]);
                     }
                     catch 
                     {
                         devClass.peiList.Add("");
                     }
                     dxfDataClass.devList.Add(devClass);
                }
               
            }
            if (kgbsum0 != 0)
            {
                ShiJiBanCount = (dxfDataClass.devList.Count - 1) / BanDevCount + 1;
                ShiJiTuZhiCount = (ShiJiBanCount - 1) / BanCount + 1;
            }
            else
            {
                ShiJiBanCount = 0;
                ShiJiTuZhiCount = 0;
            }
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }
        /// <summary>
        /// 把zpw2000信号点设备存入dxfDataClass
        /// </summary>
        /// <param name="dxfDataClass"></param>
        /// <param name="kgbNo"></param>
        /// <param name="inifile"></param>
        private int GetZPW2000AKgl(DxfDataClass dxfDataClass, string kgbNo, IniFile inifile)
        {
            int n = 0;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string[] zpw2000aDingXing = inifile.GetString("开关板", "ZPW2000A定型").Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string dingxing in zpw2000aDingXing)
            {
                string[] pair = dingxing.Split(new char[] { ',', ' ', '，' }, StringSplitOptions.RemoveEmptyEntries);
                if (pair.Length == 2)
                {
                    dic.Add(pair[0], pair[1]);
                }
            }
            n = GetZPW2000AKgl(dxfDataClass, dic, inifile, kgbNo + "\\信号点");
            return n;
        }

        private static int GetZPW2000AKgl(DxfDataClass dxfDataClass, Dictionary<string, string> dic, IniFile inifile, string sec)
        {
            DevClass devClass;// = new DevClass();
            string[] zpw2000kgls = inifile.GetString(sec, "zpw2000采集开关量信息", "").Split(new char[] { ',', ' ', '，' }, StringSplitOptions.RemoveEmptyEntries);
            int sum = inifile.GetInt(sec, "sum", 0);
            int n = 0;
            for (int i = 1; i <= sum; i++)
            {
                string[] objs = inifile.GetString(sec, i.ToString()).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (objs.Length < 2) continue;
                foreach (string kgl in zpw2000kgls)
                {

                    if (objs[1]=="0")
                    {
                        if (kgl == "-L" || kgl == "-U" || kgl == "-H") continue;
                    }
                    devClass = new DevClass();
                    n++;
                    if( (kgl == "-L" || kgl == "-U" || kgl == "-H") &&
                        (objs[0].EndsWith("B")||objs[0].EndsWith("A")||objs[0].EndsWith("C")) )
                    {
                        devClass.DevName = objs[0].Substring(0,objs[0].Length-1) + kgl;
                    }
                    else
                    {
                        devClass.DevName = objs[0] + kgl;
                    }
                    if (objs.Length == 2)
                    {
                        devClass.peiList.Add("");
                    }
                    else
                    {
                        devClass.peiList.Add(objs[2] + dic[kgl]);
                    }
                    dxfDataClass.devList.Add(devClass);
                }
            }
            return n;
        }
        public override string ReplacebNo(string str, int fi = 0)
        {
            //return base.ReplacebNo(str, fi);
            string str1 = str;
            for (int i = 0; i < BanCount; i++)
            {
                string str2 = "CX-" + (i + 2);
                if (str.Contains(str2))
                {
                    if (ShiJiBanCount == 0)
                    {
                        string s = str;
                        return s.Replace(str2, "");
                       // return s;
                    }
                    return str.Replace(str2, GetCengNo(i,fi));
                    //return str;
                }
            }
            return str1;
        }
        private string GetCengNo(int n,int fi)
        {

            int cengNo;
            int.TryParse(m_ceng.Substring(1, m_ceng.Length - 1), out cengNo);
            int m = n + fi * BanCount;
            if (m> ShiJiBanCount)
                return "";
            int n1=m + m_bNo;
            if (m_ceng == "C1")
            {
                if (n1 >= 8)
                {
                    int cn = (n1 - 8) / 8+2;
                    int ws = (n1 - 8) % 8+2;
                    return "C"+cn+"-"+ws;
                }
                else
                    return "C1-" + n1;
            }
            else
            {
                int cn = (n1 - 2) / 8 + cengNo;
                int ws = (n1 - 2) % 8 + 2;
                return "C" + cn + "-" + ws;
            }
        }
        //private string GetNextCeng(string ceng)
        //{
        //    string str = "";
        //    switch (ceng)
        //    {
        //        case "C1":
        //            str = "C2";
        //            break;
        //        case "C2":
        //            str = "C3";
        //            break;
        //        case "C3":
        //            str = "C4";
        //            break;
        //        case "C4":
        //            str = "C5";
        //            break;
        //    }
        //    m_bNo = 2;
        //    return  str;
        //}
        //public override string ReplacePei(string str, int fi = 0)
        //{
        //    for (int j = 1; j <= BanCount; j++)
        //    {
        //        int nj = j;// -fi * BanCount;
        //        for (int i = 1; i <= BanDevPeiCount * BanDevCount; i++)
        //        {
        //            string str1 = "pei" + nj.ToString() + "-" + i.ToString() + ">";
        //            if (str.Contains(str1))
        //            {
        //                int index = (fi * BanCount * BanDevCount+(j-1)*BanDevCount+i-1);// (j - 1) * BanDevCount + (i - 1) / BanDevPeiCount;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    return "";
        //                int index1 = (i - 1) % BanDevPeiCount;
        //                try
        //                {
        //                    return str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
        //                }
        //                catch (Exception e)
        //                {
        //                    return "";
        //                }
        //            }
        //        }
        //    }
        //    return str;
        //}
    }
}
